package com.rhb.af.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.rhb.af.bean.BaseQuery;
import com.rhb.af.util.BeanTools;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class GeneralDAO_Sql_SpringHibernateImpl extends HibernateDaoSupport implements
		GeneralDAO {
	
	public void save(Object obj) {}
	public void create(Object obj) {}
	public void update(Object obj) {}
	public void delete(Object obj) {}
	
	public Object findByPK(Class clazz,Serializable pk) {return null;}

	public int countByQuery(BaseQuery query) {
		Object obj = null;
		Query q = this.getSession().createSQLQuery(query.getSql(true));
		List list = q.list();
		if(list != null && !list.isEmpty()) {
			obj = list.get(0);
			return BeanTools.getInt(obj);
		}
		return 0;
	}

	public List findByQuery(BaseQuery query) {
		List<Object[]> list = this.findBySql(query.getSql(false), query.getStart(), query.getCount());
		List<Object> l = new ArrayList<Object>();
		for (Object[] obj : list) {
			l.add(query.toObject(obj));
		}
		return l;
	}

	public List<Object[]> findBySql(String sql, int start, int count) {
		Query q = this.getSession().createSQLQuery(sql);
		q.setFirstResult(start);
		q.setMaxResults(count);
		return q.list();
	}
	

}
